/*
             LUFA Library
     Copyright (C) Dean Camera, 2010.
              
  dean [at] fourwalledcubicle [dot] com
      www.fourwalledcubicle.com
*/

/*
  Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
    Additional LoopBack demo code by Opendous Inc. 2010-03-03
    For more info visit: www.Micropendous.org/LoopBack

  This firmware enumerates as a vendor-class device meaning
  the developer must decide and code a communication scheme.
  Examples of arbitrary endpoint as well as control endpoint
  communication are given.
  Search for TODO statements for implementation hints.
  Avoid busy loops as the USB task is not preemptive and USB
  has timing contraints.  Use concurrent programming techniques.
  For example, have one function set a flag when an event occurs.
  Have another function do significant processing only if the flag is set.
  Have both functions run from main()'s for(;;) loop, which should be the
  only indefinite loop in your firmware.

  Permission to use, copy, modify, and distribute this software
  and its documentation for any purpose and without fee is hereby
  granted, provided that the above copyright notice appear in all
  copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

#include "usbtointDescriptors.h"


const USB_Descriptor_Device_t PROGMEM DeviceDescriptor =
{
	.Header                 = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
	

         /* ToDo use the LUFA makros */  	
	.USBSpecification       = VERSION_BCD(01.10),
	.Class                  = 0x00, //USB_CSCP_VendorSpecificClass,
	.SubClass               = 0x00, //USB_CSCP_NoDeviceSubclass,
	.Protocol               = 0x00, //USB_CSCP_NoDeviceProtocol,
				
	.Endpoint0Size          = 8,//FIXED_CONTROL_ENDPOINT_SIZE,
		
	.VendorID               = 0x03EB,
	.ProductID              = 0x2040,
	.ReleaseNumber          = 0x0001,


	.ManufacturerStrIndex   = 0x01,
	.ProductStrIndex        = 0x02,
	.SerialNumStrIndex      = 0x03,//USE_INTERNAL_SERIAL,
		
	.NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
};

const USB_Descriptor_Configuration_t PROGMEM ConfigurationDescriptor =
{
	.Config = 
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},

			.TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
			.TotalInterfaces        = 1,
				
			.ConfigurationNumber    = 1,
			.ConfigurationStrIndex  = NO_DESCRIPTOR,
				
			.ConfigAttributes       = (USB_CONFIG_ATTR_RESERVED | USB_CONFIG_ATTR_SELFPOWERED),
			
			.MaxPowerConsumption    = USB_CONFIG_POWER_MA(100)
		},
		
	.Interface = 
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},

			.InterfaceNumber        = 0x00,
			.AlternateSetting       = 0x00,
			
			.TotalEndpoints         = 2,
				
			.Class                  = 0xff,  //USB_CSCP_VendorSpecificClass,  
			.SubClass               = 0x00,  //USB_CSCP_NoDeviceSubclass,
			.Protocol               = 0x00,  //USB_CSCP_NoDeviceProtocol,
				
			.InterfaceStrIndex      = NO_DESCRIPTOR
		},

	.DataOUTEndpoint = 
		{
			.Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},

			.EndpointAddress        = (ENDPOINT_DIR_OUT | DATA_OUT_EPADDR),
			.Attributes             = EP_TYPE_INTERRUPT,
			.EndpointSize           = DATA_OUT_EPSIZE,
			.PollingIntervalMS      = 0x01
		},
     
	.DataINEndpoint =
	       {
                        .Header                 = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
                       
                        
			.EndpointAddress        = (ENDPOINT_DIR_IN | DATA_IN_EPADDR),
			.Attributes             = EP_TYPE_INTERRUPT,
			.EndpointSize           = DATA_IN_EPSIZE,
			.PollingIntervalMS      = 0x01
	       }
};

const USB_Descriptor_String_t PROGMEM LanguageString =
{
	.Header                 = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
		
	.UnicodeString          = {LANGUAGE_ID_ENG}
};

const USB_Descriptor_String_t PROGMEM ManufacturerString =
{
	.Header                 = {.Size = USB_STRING_LEN(12), .Type = DTYPE_String},
		
	.UnicodeString          = L"kernschmiede"
};

const USB_Descriptor_String_t PROGMEM ProductString =
{
	.Header                 = {.Size = USB_STRING_LEN(13), .Type = DTYPE_String},
		
	.UnicodeString          = L"kernschmiede"
};

const USB_Descriptor_String_t PROGMEM SerialNumberString  =
{
	.Header                 = {.Size = USB_STRING_LEN(12), .Type = DTYPE_String},
		
	.UnicodeString          = L"000000000000"
};

uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
                                    const uint8_t wIndex,
                                    const void** const DescriptorAddress)
{
	const uint8_t  DescriptorType   = (wValue >> 8);
	const uint8_t  DescriptorNumber = (wValue & 0xFF);

	const void* Address = NULL;
	uint16_t    Size    = NO_DESCRIPTOR;

	switch (DescriptorType)
	{
		case DTYPE_Device:
			Address = &DeviceDescriptor;
			Size    = sizeof(USB_Descriptor_Device_t);
			break;
		case DTYPE_Configuration:
			Address = &ConfigurationDescriptor;
			Size    = sizeof(USB_Descriptor_Configuration_t);
			break;
		case DTYPE_String:
			switch (DescriptorNumber)
			{
				case 0x00:
					Address = &LanguageString;
					Size    = pgm_read_byte(&LanguageString.Header.Size);
					break;
				case 0x01:
					Address = &ManufacturerString;
					Size    = pgm_read_byte(&ManufacturerString.Header.Size);
					break;
				case 0x02:
					Address = &ProductString;
					Size    = pgm_read_byte(&ProductString.Header.Size);
					break;
			}

			break;
	}

	*DescriptorAddress = Address;
        return Size;
}
